#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include "../Include/log_pub.h"  

#define STUDENT_FILE "Data/student.txt"  
#define LOG_FILE "log.log"  

typedef struct {  
    char id[10];  
    char name[50];  
    char course_list[100]; // 存储已选课程  
    int fraction; // 总学分  
} Student;    

void add_Student() {  // 添加学生  
    Student student;
    student.course_list[0] = '\0'; // 初始化课程列表  
    student.fraction = 0; // 初始化学分    
    printf("请输入学生ID: ");  
    scanf("%s", student.id);  
    printf("请输入学生姓名: ");  
    scanf("%s", student.name);  
    printf("请输入学生选课: ");  
    scanf("%s", student.course_list); 
    printf("请输入学生总学分: ");  
    scanf("%d", &student.fraction); 
    

    FILE *file = fopen(STUDENT_FILE, "a");  
    if (file) {  
        fwrite(&student, sizeof(Student), 1, file);  
        fclose(file);  
        log_message("添加学生");  
        printf("学生添加成功！\n");  
    } else {  
        printf("打开文件时发生错误！\n");  
    }  
}  

void add_Student_import() {  
    // 假设导入功能是从一个文本文件导入学生信息，格式为每行一个学生，含ID和名字  
    FILE *input_file = fopen("Data/import_students.txt", "r");  
    if (!input_file) {  
        printf("打开导入文件时发生错误！\n");  
        return;  
    }  

    Student student;  
    while (1) {  
        student.course_list[0] = '\0'; // 初始化课程列表  
        student.fraction = 0; // 初始化学分  
        if(fscanf(input_file, "%s %s %s %d", student.id, student.name, student.course_list,&student.fraction) == EOF)
            break;

        FILE *file = fopen(STUDENT_FILE, "a");  
        if (file) {  
            fwrite(&student, sizeof(Student), 1, file);  
            fclose(file);  
            log_message("导入学生");  
        }  
    }  

    fclose(input_file);  
    printf("学生导入成功！\n");  
}  

void delecte_Student() {  // 删除学生  
    char id[10];  
    printf("请输入要删除的学生ID: ");  
    scanf("%s", id);  

    Student student;  
    FILE *file = fopen(STUDENT_FILE, "r");  
    FILE *temp_file = fopen("Data/temp.txt", "w");  
    int found = 0;  

    if (file && temp_file) {  
        while (fread(&student, sizeof(Student), 1, file)) {  
            if (strcmp(student.id, id) != 0) {  
                fwrite(&student, sizeof(Student), 1, temp_file);  
            } else {  
                found = 1;  
            }  
        }  
        fclose(file);  
        fclose(temp_file);  
        remove(STUDENT_FILE);  
        rename("Data/temp.txt", STUDENT_FILE);  
        if (found) {  
            log_message("删除学生");  
            printf("学生删除成功！\n");  
        } else {  
            printf("未找到该学生！\n");  
        }  
    } else {  
        printf("打开文件时发生错误！\n");  
    }  
}  

void view_Student() {  // 查看学生  
    Student student;  
    FILE *file = fopen(STUDENT_FILE, "r");  
    if (file) {  
        while (fread(&student, sizeof(Student), 1, file)) {  
            printf("ID: %s, 姓名: %s, 选课: %s, 总学分: %d\n", student.id, student.name, student.course_list, student.fraction);  
        }  
        fclose(file);  
    } else {  
        printf("打开文件时发生错误！\n");  
    }  
}  

void id_View_Student() {  // ID搜索  
    char id[10];  
    printf("请输入要查看的学生ID: ");  
    scanf("%s", id);  

    Student student;  
    FILE *file = fopen(STUDENT_FILE, "r");  
    if (file) {  
        while (fread(&student, sizeof(Student), 1, file)) {  
            if (strcmp(student.id, id) == 0) {  
                                printf("ID: %s, 姓名: %s, 选课: %s, 总学分: %d\n", student.id, student.name, student.course_list, student.fraction);  
                fclose(file);  
                return;  
            }  
        }  
        fclose(file);  
        printf("未找到该学生！\n");  
    } else {  
        printf("打开文件时发生错误！\n");  
    }  
}  

void name_view_Student() {  //名字搜索
    char name[50];  
    printf("输入查询的学生姓名:");  
    scanf("%s", name);  

    Student student;  
    FILE *file = fopen(STUDENT_FILE, "r");  
    if (file) {  
        int found = 0;  
        while (fread(&student, sizeof(Student), 1, file)) {  
            if (strcmp(student.name, name) == 0) {  
                printf("ID: %s, Name: %s, Courses: %s, Total Credits: %d\n", student.id, student.name, student.course_list, student.fraction);  
                found = 1;  
            }  
        }  
        if (!found) {  
            printf("Student not found!\n");  
        }  
        fclose(file);  
    } else {  
        printf("Error opening file!\n");  
    }  
} 

void revise_Student() {  // 修改学生信息  
    char id[10];  
    printf("请输入要修改的学生ID: ");  
    scanf("%s", id);  

    Student student;  
    FILE *file = fopen(STUDENT_FILE, "r");  
    FILE *temp_file = fopen("Data/temp.txt", "w");  
    int found = 0;  

    if (file && temp_file) {  
        while (fread(&student, sizeof(Student), 1, file)) {  
            if (strcmp(student.id, id) == 0) {  
                printf("请输入新姓名: ");  
                scanf("%s", student.name);  
                found = 1;  
            }  
            fwrite(&student, sizeof(Student), 1, temp_file);  
        }  
        fclose(file);  
        fclose(temp_file);  
        remove(STUDENT_FILE);  
        rename("Data/temp.txt", STUDENT_FILE);  
        if (found) {  
            log_message("修改学生信息");  
            printf("学生信息修改成功！\n");  
        } else {  
            printf("未找到该学生！\n");  
        }  
    } else {  
        printf("打开文件时发生错误！\n");  
    }  
}  

void name_Revise_Student() {  // 修改学生姓名  
    char old_name[50], new_name[50];  
    printf("请输入要修改的旧姓名: ");  
    scanf("%s", old_name);  
    printf("请输入新姓名: ");  
    scanf("%s", new_name);  

    Student student;  
    FILE *file = fopen(STUDENT_FILE, "r");  
    FILE *temp_file = fopen("Data/temp.txt", "w");  
    int found = 0;  

    if (file && temp_file) {  
        while (fread(&student, sizeof(Student), 1, file)) {  
            if (strcmp(student.name, old_name) == 0) {  
                strcpy(student.name, new_name);  
                found = 1;  
            }  
            fwrite(&student, sizeof(Student), 1, temp_file);  
        }  
        fclose(file);  
        fclose(temp_file);  
        remove(STUDENT_FILE);  
        rename("Data/temp.txt", STUDENT_FILE);  
        if (found) {  
            log_message("修改学生姓名");  
            printf("学生姓名修改成功！\n");  
        } else {  
            printf("未找到该学生！\n");  
        }  
    } else {  
        printf("打开文件时发生错误！\n");  
    }  
}  

void id_Revise_Student() {  // 修改学生ID  
    char id[10];  
    printf("请输入要修改的学生ID: ");  
    scanf("%s", id);  

    Student student;  
    FILE *file = fopen(STUDENT_FILE, "r");  
    FILE *temp_file = fopen("Data/temp.txt", "w");  
    int found = 0;  

    if (file && temp_file) {  
        while (fread(&student, sizeof(Student), 1, file)) {  
            if (strcmp(student.id, id) == 0) {  
                printf("请输入新ID: ");  
                scanf("%s", student.id);  
                found = 1;  
            }  
            fwrite(&student, sizeof(Student), 1, temp_file);  
        }  
        fclose(file);  
        fclose(temp_file);  
        remove(STUDENT_FILE);  
        rename("Data/temp.txt", STUDENT_FILE);  
        if (found) {  
            log_message("修改学生ID");  
            printf("学生ID修改成功！\n");  
        } else {  
            printf("未找到该学生！\n");  
        }  
    } else {  
        printf("打开文件时发生错误！\n");  
    }  
}